
/* jshint esversion: 6 */
import React from 'react';
import { List } from 'antd-mobile';
import {
    Form,
    Input,
    Button,
    Toast
} from 'antd-mobile';

import Qs from 'qs';
import cookie from 'react-cookies';
import axios from "../../http";

import {withRouter} from "react-router";
class Login extends React.Component {
    constructor(props) {
        super(props);
       this.state={username: '', password: ''};
    }

    usernameOnChange=(value)=>{
        this.setState({username:value});
    };
    passwordOnChange=(value)=>{
        this.setState({password:value});
    };

    handleLogin=() => {
        axios.post('api/auth/oauth/token?grant_type=password', Qs.stringify({
            username: this.state.username,
            password: this.state.password
        }), {headers: {'Content-Type': 'application/x-www-form-urlencoded'}})
            .then((response) => {
                let data = response.data;
                if (data.access_token !==undefined) {
                    cookie.save('access_token', data.access_token);
                   /* let history = createMemoryHistory();
                    history.push({
                        pathname: "/main"
                    });
                    history.go();*/
                    //return <Redirect to={{pathname:"/main"}} />

                    this.props.history.push('/main')
                } else {

                    let messageDic = {};
                    messageDic["Bad credentials"] = "用户名或密码错误";
                    messageDic["User account is locked"] = "用户被锁定，请联系管理员解锁。";
                    messageDic["Maximum sessions of 1 for this principal exceeded"] = "该用户已在别处登录，请先退出后再登录，或联系管理员踢除已登录账户。";
                    messageDic["User credentials have expired"] = "密码已过期。";
                    messageDic["User account has expired"] = "账号已过期。";
                    Toast.show({
                        icon: 'fail',
                        content: messageDic[data.error_description],
                    });

                }
            }).catch(function (error) {

        });
    }
    render() {
        return (
            <List mode='card'>
                <List.Item title='用户登录'>
                    <Form
                        layout='horizontal'
                        footer={
                            <Button block type='submit' color='primary' onClick={this.handleLogin}>
                                提交
                            </Button>
                        }
                    >
                        <Form.Item
                            name='用户名'
                            label='用户名:'
                            rules={[{ required: true, message: '用户名不能为空' }]}
                        >
                            <Input  onChange={this.usernameOnChange} placeholder='请输入用户名' />
                        </Form.Item>
                        <Form.Item name='address' label='密码:'>
                            <Input onChange={this.passwordOnChange} placeholder='请输入密码' type='password' />
                        </Form.Item>
                    </Form>

                </List.Item>

            </List>

        );
    }
}
// this wrapper is for fixing bug that we can not get this.props.location
export default withRouter(Login)